package com.fansz.FanszAPI.utils;

import java.security.MessageDigest;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

public class StringUtils {

    // get client real IP
    public static String getRemoteIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    // MD5 hash
    public final static String MD5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // XSS filter
    public static String getSearchWord(String word) {
        word = word.trim();
        word = word.replace('<', ' ');
        word = word.replace('>', ' ');
        word = word.replace('"', ' ');
        word = word.replace('\'', ' ');
        word = word.replace('/', ' ');
        word = word.replace('%', ' ');
        word = word.replace(';', ' ');
        word = word.replace('(', ' ');
        word = word.replace(')', ' ');
        word = word.replace('&', ' ');
        word = word.replace('+', '_');
        return word;
    }

    public static boolean isNotNull(String str)
    {
        if (str != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public static boolean isEmpty(String str)
    {
        if (str == null || "".equals(str))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static boolean isEmpty(List<String> str)
    {
        if (str == null || str.size() == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static boolean isEmptyAndLen(String str, int length)
    {
        if (str == null || "".equals(str) || str.length() > length)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static boolean checkLen(String str, int length)
    {
        if (str != null && str.length() > length)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public static int getRandomNum()
    {
        Random random = new Random();
        return random.nextInt(899999)+100000;
    }

    public static boolean isNumber(String str)
    {
        boolean result;
        try {
            Integer.parseInt(str);
            result = true;
        } catch (Exception e)
        {
            result = false;
        }

        return result;
    }
}